package com.ishare.admin.controller;

import com.ishare.admin.service.SysRoleMenuService;
import com.ishare.admin.service.SysRoleService;
import com.ishare.admin.service.SysUserRoleService;
import com.ishare.common.base.BaseController;
import com.ishare.common.entity.dto.BatchDto;
import com.ishare.common.entity.po.SysRole;
import com.ishare.common.page.PageUtils;
import com.ishare.common.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags = {"管理后台角色"})
public class SysRoleController extends BaseController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 角色列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("operate:role:list")
    @ApiOperation(value = "角色列表",notes = "")
    public Wrapper list(@RequestParam Map<String, Object> params) {
        //params.put("createBy", getUserId());
        PageUtils page = sysRoleService.queryPage(params);

        return Wrapper.ok(page);
    }

    /**
     * 角色列表
     */
    @GetMapping("/select")
    @ApiOperation(value = "拥有的角色列表",notes = "")
    public Wrapper select() {
        Map<String, Object> map = new HashMap<>(4);
        List<SysRole> list = sysRoleService.list();
        List<Long> ownRoleIdList = sysUserRoleService.queryRoleIdList(getUserId());
        map.put("roleList",list);
        map.put("ownRoleIdList",ownRoleIdList);
        return Wrapper.ok(map);
    }

    /**
     * 角色信息
     */
    @GetMapping("/info/{roleId}")
    @ApiOperation(value = "角色详情",notes = "")
    public Wrapper info(@PathVariable("roleId") Long roleId) {
        SysRole role = sysRoleService.getById(roleId);

        //查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        return Wrapper.ok(role);
    }

    /**
     * 保存角色
     */
    //@SysLog("保存角色")
    @PostMapping("/save")
    //@RequiresPermissions("operate:role:save")
    @ApiOperation(value = "保存角色",notes = "")
    public Wrapper save(@RequestBody SysRole role) {
        sysRoleService.saveRole(role);

        return Wrapper.ok();
    }

    /**
     * 修改角色
     */
    //@SysLog("修改角色")
    @PostMapping("/update")
    //@RequiresPermissions("operate:role:update")
    @ApiOperation(value = "修改角色",notes = "")
    public Wrapper update(@RequestBody SysRole role) {

        sysRoleService.update(role);

        return Wrapper.ok();
    }

    /**
     * 删除角色
     */
    //@SysLog("删除角色")
    @PostMapping("/delete")
    //@RequiresPermissions("operate:role:delete")
    @ApiOperation(value = "删除角色",notes = "")
    public Wrapper delete(@RequestBody BatchDto batchDto) {
        sysRoleService.deleteBatch(batchDto.getIds());

        return Wrapper.ok();
    }
}
